package com.map;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;


import com.asy.ui.base.ImageAndText;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;


public class GeoItemizedOverlay extends ItemizedOverlay<MapoverlayItem> {
	
	private Resources resources;
	private int Rid;
	private ArrayList<MapoverlayItem> mOverlays = new ArrayList<MapoverlayItem>();
	
	public GeoItemizedOverlay(Drawable defaultMarker,Resources resources,int rid) {
		super(defaultMarker);
		this.resources=resources;
		this.Rid=rid;
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected MapoverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mOverlays.get(i);
	}
	
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}
	
	public void addOverlay(MapoverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}
	
	
	private void drawPoint(Canvas canvas,MapoverlayItem overLayItem,Projection projection)
	{
		Point point = projection.toPixels(overLayItem.getPoint(), null);
		Bitmap bmp = BitmapFactory.decodeResource(resources, Rid);            
        canvas.drawBitmap(bmp, point.x-2, point.y-39, null);   
		Paint paintText = new Paint();
		paintText.setColor(Color.RED);
		paintText.setTextSize(25);
	}
	
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		Projection projection = mapView.getProjection();
		if(mapView.getZoomLevel()>=17)
		{
			for (int index = 0; index <mOverlays.size(); index++) {
				MapoverlayItem overLayItem = getItem(index);
				overLayItem.setIstap(true);
				overLayItem.setIsupdatedata(false);
				drawPoint(canvas,overLayItem,projection);
			}
		}
		else {
			HashSet<String> hashSet=new HashSet<String>();
			List<List<MapoverlayItem>> list=new ArrayList<List<MapoverlayItem>>();
			for (int i = 0; i < mOverlays.size(); i++) {
				MapoverlayItem mapoverlayItem=getItem(i);
				if(hashSet.contains(mapoverlayItem.getKey()))
				{
					continue;
				}
				Point point = projection.toPixels(mapoverlayItem.getPoint(), null);
				List<MapoverlayItem> mapList=new ArrayList<MapoverlayItem>();
				for (int j = 0; j < mOverlays.size(); j++) {
					if(i!=j && !hashSet.contains(getItem(j).getKey()))
					{
						Point npoint=projection.toPixels(getItem(j).getPoint(), null);
						int xran=point.x-npoint.x;
						int yran=point.y-npoint.y;
						if((xran<=15 && xran>=-15)&&(yran<=30 && yran>=-30))
						{
						   mapList.add(getItem(j));
						   hashSet.add(getItem(j).getKey());
						}
					}
				}
				mapList.add(getItem(i));
			    hashSet.add(getItem(i).getKey());
			    list.add(mapList);
			}
			
			for (int i = 0; i < list.size(); i++) {
				
				if(list.get(i).size()==1)
				{
					MapoverlayItem mapoverlayItem=list.get(i).get(0);
					mapoverlayItem.setIstap(true);
					mapoverlayItem.setIsupdatedata(true);
					mapoverlayItem.setShowlist(mapoverlayItem.getMaplist());
					drawPoint(canvas,mapoverlayItem,projection);
				}
				else if(list.get(i).size()==2) {
					MapoverlayItem o1MapoverlayItem=list.get(i).get(0);
					MapoverlayItem o2MapoverlayItem=list.get(i).get(1);
					Point p1=projection.toPixels(o1MapoverlayItem.getPoint(), null);
					Point p2=projection.toPixels(o2MapoverlayItem.getPoint(), null);
					int x=(p1.x+p2.x)/2;
					int y=(p1.y+p2.y)/2;
					List<ImageAndText> imaList=new ArrayList<ImageAndText>();
					imaList.addAll(o1MapoverlayItem.getMaplist());
					imaList.addAll(o2MapoverlayItem.getMaplist());
					if((p1.x-x)<(p2.x-x) && (p1.y-y)<(p2.y-y))
					{
						o1MapoverlayItem.setIstap(true);
						o1MapoverlayItem.setIsupdatedata(true);
						o1MapoverlayItem.setShowlist(imaList);
						o2MapoverlayItem.setIstap(false);
						o2MapoverlayItem.setIsupdatedata(false);
						drawPoint(canvas,o1MapoverlayItem,projection);
					}
					else {
						
						o2MapoverlayItem.setIstap(true);
						o2MapoverlayItem.setIsupdatedata(true);
						o2MapoverlayItem.setShowlist(imaList);
						o1MapoverlayItem.setIstap(false);
						o1MapoverlayItem.setIsupdatedata(false);
						drawPoint(canvas,o2MapoverlayItem,projection);
					}
				}
				else {
					int xsum=0;
					int ysum=0;
					List<ImageAndText> imagelist=new ArrayList<ImageAndText>();
					
					for (int j = 0; j < list.get(i).size(); j++) {
						Point sp=projection.toPixels(list.get(i).get(j).getPoint(), null);
						list.get(i).get(j).setIstap(false);
						list.get(i).get(j).setIsupdatedata(false);
						xsum+=sp.x;
						ysum+=sp.y;
						imagelist.addAll(list.get(i).get(j).getMaplist());
					}
					int xavg=xsum/list.get(i).size();
					int yavg=ysum/list.get(i).size();
					
					Object temp;
					Object showObject=null;
					
				 	Object[] data=list.get(i).toArray();
				 	
					for (int j = 0; j < data.length; j++) {
						int lowIndex=j;
						MapoverlayItem lowMapoverlayItem=(MapoverlayItem)data[lowIndex];
						Point lowpoint=projection.toPixels(lowMapoverlayItem.getPoint(), null);
						for (int k = data.length-1; k >j; k--) {
							MapoverlayItem kMapoverlayItem=(MapoverlayItem)data[k];
							Point kpoint=projection.toPixels(kMapoverlayItem.getPoint(), null);
							if((kpoint.x-xavg)>(lowpoint.x-xavg)&&(kpoint.y-yavg)>(lowpoint.y-yavg))
							{
								lowIndex=k;
							}
						}
						temp=data[j];
						data[j]=data[lowIndex];
						data[lowIndex]=temp;
						showObject=data[lowIndex];
					}
					if(showObject!=null)
					{
						MapoverlayItem showMapoverlayItem=(MapoverlayItem)showObject;
						showMapoverlayItem.setIstap(true);
						showMapoverlayItem.setIsupdatedata(true);
						showMapoverlayItem.setShowlist(imagelist);
						drawPoint(canvas,showMapoverlayItem,projection);
					}
				}
			}		
//			HashSet<String> hashSet=new HashSet<String>();
//			for (int i = 0; i < mOverlays.size(); i++) {
//				MapoverlayItem mapoverlayItem=getItem(i);
//				if(hashSet.contains(mapoverlayItem.getKey()))
//				{
//					mapoverlayItem.setIstap(false);
//					mapoverlayItem.setIsupdatedata(false);
//					continue;
//				}
//				else if(i==mOverlays.size()-1) {
//					mapoverlayItem.setIstap(false);
//					mapoverlayItem.setIsupdatedata(false);
//				}
//				Point point = projection.toPixels(mapoverlayItem.getPoint(), null);
//				List<ImageAndText> imageList=new ArrayList<ImageAndText>();
//				for (int j = 0; j < mOverlays.size(); j++) {
//					if(j<mOverlays.size()&&!hashSet.contains(getItem(j).getKey()) &&j!=i)
//					{
//						Point npoint=projection.toPixels(getItem(j).getPoint(), null);
//						int xran=point.x-npoint.x;
//						int yran=point.y-npoint.y;
//						if((xran<=15 && xran>=-15)&&(yran<=30 && yran>=-30))
//						{
//						   imageList.addAll((getItem(j).getMaplist()));
//						   hashSet.add(getItem(j).getKey());
//						}
//					}
//				}
//				if(imageList.size()>0)
//				{
//					imageList.addAll(mapoverlayItem.getMaplist());
//					mapoverlayItem.setIstap(true);
//					mapoverlayItem.setIsupdatedata(true);
//					mapoverlayItem.setShowlist(imageList);
//				}
//				else {
//					mapoverlayItem.setIstap(true);
//					mapoverlayItem.setIsupdatedata(false);
//				}
//				drawPoint(canvas,mapoverlayItem,projection);		
//			}
		}
		super.draw(canvas, mapView, shadow);
	}

	
	
	
	
	
}
